home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12830 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  79 lines

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help!  Can't read input from keyboard!
  5. Date: 2 Apr 1996 18:40:12 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Message-ID: <4jshdc$q4e@solutions.solon.com>
  8. References: <4jq0ts$5mh@taniemarie.solon.com> <4jre6e$op1@linet06.li.net>
  9. NNTP-Posting-Host: solutions.solon.com
  10.  
  11. In article <4jre6e$op1@linet06.li.net>,
  12. Jeremy Markman <jeremy@newshost.li.net> wrote:
  13. >Peter Seebach (seebs@taniemarie.solon.com) wrote:
  14. >: I guess I thought I knew C, but maybe not.  The following program compiles
  15.  
  16. >Maybe Not...
  17.  
  18. >First, the sscanf() function will read in input from a string.  You did 
  19. >not specify a string, which is why you got an error originally when 
  20. >compiling.
  21.  
  22. I did *NOT* get an error compiling; the code, as posted originally,
  23. *does* compile.  Did you even *try* it?
  24.  
  25. Not only did I never call sscanf, but I certainly *did* specify a string;
  26. I specified "3" as a string.
  27.  
  28. >Use the getch() function to read in one character 
  29. >at a time from the keyboard, then parse the characters you read in to 
  30. >make sure you are getting the input you are looking for.
  31.  
  32. This is unfortunately very *bad* advice.  getch() is not a function in
  33. the C language, and is not available on my system.
  34.  
  35. >Here's a quick example:
  36.  
  37. >int main()
  38. > { 
  39. >  int ch;
  40.  
  41. >  ch = getch();
  42. >  printf("%c\n",ch);
  43.  
  44. >  return(0);
  45. > }
  46.  
  47. This gets
  48.     ld: main.o: undefined symbol _getch()
  49. on at least one machine.
  50.  
  51. You should use "getchar" if you want to do this.  You will notice that
  52. getchar does not return characters until the user sends the line; this
  53. is the only behavior you can have in legal and conforming C, and there's
  54. no C way to change it.  (The behavior of getch() you depend on is specific
  55. to one OS, and is not the default on many other systems, even ones which
  56. happen to have a function called getch.)
  57.  
  58. >Old Code:
  59. >:     int main(void) {
  60. >:         int i = 0;
  61. >:         (void) sscanf, "3", "%d", i;
  62.  
  63. This compiles just fine, you realize.
  64.  
  65. >:         (void) printf, "%d", i;
  66. >:         return 0;
  67. >:     }
  68.  
  69. Please do *NOT* post answers if you haven't got a clue what the question was.
  70.  
  71. And happy April Fools' day...
  72.  
  73. -s
  74. -- 
  75. Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
  76. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  77. FUCK the communications decency act.  Goddamned government.  [literally.]
  78. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
  79.